﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSTOCK
{
    public partial class FMS_TS_EQUIPMENT_INFO : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private int iGridView = 0;
        private string sType = "V";
        private string sSite_No = string.Empty;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();

        public FMS_TS_EQUIPMENT_INFO()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = " select b.fdictname,a.* "
              +" from s_ts_equipment_info a,s_ts_equipment_dict b"
              +" where a.fequipmenttype = b.fdictno";
            DataRowView dr1 = (DataRowView)cbfequipmenttype.SelectedItem;
            if ((dr1 != null) && (cbfequipmenttype.SelectedIndex > 0))
            {
                SQL = SQL + " and a.fequipmenttype=" + dr1.Row["fdictno"].ToString();
            }
            if (uifcasno.Text != "")
            {
                SQL = SQL + " and ((a.fequipmentno like '%" + uifcasno.Text + "%') or (a.fequipmentname like '%" + uifcasno.Text + "%'))";
            }
            SQL = SQL + "  order by a.fequipmenttype,a.fequipmentno";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            rowindex = -1;
            
        }

        private void FMS_TS_EQUIPMENT_INFO_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "import_s_ts_equipment_info";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys03.Visible = false; sys03.Tag = "1";
            sys05.Visible = false; sys05.Tag = "1";
            sys06.Visible = false; sys06.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            //下拉框
            DataTable myTs_Dict = myCache.doGetDict("S_TS_EQUIPMENT_DICT", "");
            uifequipmenttype.DataSource = myTs_Dict;
            uifequipmenttype.DisplayMember = "fdictname";
            uifequipmenttype.ValueMember = "fdictno";

            DataRow dr1 = myTs_Dict.NewRow();
            dr1["fdictname"] = "请选择";
            dr1["fdictno"] = "0";
            myTs_Dict.Rows.InsertAt(dr1, 0);
            cbfequipmenttype.DataSource = myTs_Dict;
            cbfequipmenttype.DisplayMember = "fdictname";
            cbfequipmenttype.ValueMember = "fdictno";
            cbfequipmenttype.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //新增代码
        protected override void OnSys01()
        {
            uifequipmentno.Text = "";
            uifequipmentname.Text = "";
            uifunit.Text = "台";
            uifbrand.Text = "合极";
            uifspec.Text = "6000";
            uifequipmenttype.SelectedIndex = 0;

            DataTable myNo = StaticClass.myDB.query("select ISNULL(max(feuipmentid),0)+1 from s_ts_equipment_info");
            if ((myNo != null) && (myNo.Rows.Count > 0))
            {
                uifeuipmentid.Text = myNo.Rows[0][0].ToString();
            }
            else
            {
                UIMessageTip.ShowOk("初始化报错");
                return;
            }
            sType = "A";
            doLoadData();
            uifequipmentno.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            //控制界面输入
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uifequipmentno.Text = uiDGView["fequipmentno", rowindex].Value.ToString();
                uifequipmentname.Text = uiDGView["fequipmentname", rowindex].Value.ToString();
                uifunit.Text = uiDGView["funit", rowindex].Value.ToString();
                uifbrand.Text = uiDGView["fbrand", rowindex].Value.ToString();
                uifspec.Text = uiDGView["fspec", rowindex].Value.ToString();
                uifeuipmentid.Text = uiDGView["feuipmentid", rowindex].Value.ToString();

                int i = 0;
                string sstatus = uiDGView["fequipmenttype", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifequipmenttype.Items)
                {
                    string id = dr["fdictno"].ToString();
                    if (id.Equals(sstatus))
                    {
                        uifequipmenttype.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

            }

        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "B";

            doLoadData();

            uifequipmentno.ReadOnly = true;

            base.OnSys02();
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();

            base.OnSys10();

        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifequipmentno, "请输入设备编号")
                   && CheckEmpty(uifequipmentname, "请输入设备名称")
                   && CheckEmpty(uifspec, "请输入设备型号")
                   && CheckEmpty(uifequipmenttype, "请选择设备类型");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }
            DataRowView drv1 = (DataRowView)uifequipmenttype.SelectedItem;
            string sfequipmenttype = drv1.Row["fDictNo"].ToString();

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        s_ts_equipment_info myData = context.s_ts_equipment_info.Find(int.Parse(uifeuipmentid.Text));
                        if (myData is null)
                        {
                            s_ts_equipment_info myData2 = new s_ts_equipment_info();
                            myData2.feuipmentid =int.Parse(uifeuipmentid.Text);
                            myData2.fequipmentno = uifequipmentno.Text;
                            myData2.fequipmentname = uifequipmentname.Text;
                            myData2.funit = uifunit.Text;
                            myData2.fspec = uifspec.Text;
                            myData2.foperator = sUser;
                            myData2.fupddate = DateTime.Now;
                            myData2.fequipmenttype = int.Parse(sfequipmenttype);
                            myData2.fclassstno = "0";
                            myData2.fpanel = 0;
                            myData2.fuseyear = 5;
                            myData2.fbuyprice = 0;
                            myData2.flowvoltage = 0;
                            myData2.fheightvoltage = 0;
                            context.s_ts_equipment_info.Add(myData2);
                            context.SaveChanges();

                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    s_ts_equipment_info myts_equipinfo = context.s_ts_equipment_info.Find(int.Parse(uifeuipmentid.Text));

                    myts_equipinfo.fequipmentname = uifequipmentname.Text;
                    myts_equipinfo.funit = uifunit.Text;
                    myts_equipinfo.fspec = uifspec.Text;
                    myts_equipinfo.foperator = sUser;
                    myts_equipinfo.fupddate = DateTime.Now;
                    int result = context.SaveChanges();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

        private void cbfequipmenttype_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
